<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 商户 代理商
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Admin;

use App\Tools\Prstatic;
use Illuminate\Database\Eloquent\Model;

class GoodsStore extends Model
{

    protected $table = 'goods_store';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳

    /**
     * 批量添加商户的商品价格信息
     * @param $store_id
     * @param $listArr
     */
    public static function adminAddInfo($store_id, $listArr)
    {
        $nowTime = time();
        if ($store_id && $listArr) {
            foreach ($listArr as $key => $item) {
                $listArr[$key]['store_id'] = $store_id;
                $listArr[$key]['created_at'] = $nowTime;
            }
            return self::insert($listArr);
        }
        return false;
    }

    /**
     * 批量编辑处理
     * @param $store_id
     * @param $listArr
     * @return false
     */
    public static function adminEditInfo($store_id, $listArr)
    {
        $nowTime = time();
        if ($store_id && $listArr) {
            foreach ($listArr as $key => $item) {
                self::where(['store_id' => $store_id, 'goods_id' => $item['goods_id']])->update($item);
            }
            return true;
        }
        return false;
    }

    /**
     * 获取单品信息
     * @param $store_id
     * @param $goods_id
     */
    public static function getInfoBySid($store_id, $goods_id)
    {
        $info = self::where(['store_id' => $store_id, 'goods_id' => $goods_id])->first();
        return $info;
    }

    /**
     * 商户商品库存
     * @return mixed
     */
    public static function getRepertoryList($store_id)
    {
        $list = self::from('goods_store as gs')
            ->select(['g.id', 'g.goods_sn', 'g.goods_name', 'gs.sale_price', 'g.fixed_price', 'c.cate_name', 'g.goods_total_num', 'g.goods_sale_num'])
            ->leftJoin('goods as g', 'g.id', '=', 'gs.goods_id')
            ->leftJoin('goods_category as c', 'c.id', '=', 'g.cate_id')
            ->where(['gs.store_id' => $store_id])
            ->get();
        foreach ($list as $key => $vv) {
            if ($vv['id'] == null) {
                unset($list[$key]);
                continue;
            }
            $list[$key]['goods_price'] = $vv['sale_price'];
        }
        return $list;
    }

    /**
     * 获取订单提交的商品
     */
    public static function getOrderByGoodsId($store_id, $goods_id, $num = 1)
    {
        $goodsInfo = self::from('goods_store as gs')
            ->leftJoin('goods as g', 'g.id', '=', 'gs.goods_id')
            ->where(['gs.store_id' => $store_id, 'gs.goods_id' => $goods_id])
            ->select(['gs.goods_id', 'g.goods_img', 'g.goods_name', 'g.mini_quantity', 'gs.sale_price', 'gs.buy_num', 'gs.get_num'])->first();
        if ($goodsInfo) {
            //            $num = ($num < $goodsInfo['mini_quantity']) ? $goodsInfo['mini_quantity'] : $num;
            $pamas['goods_list'][] = ['goods_id' => $goodsInfo['goods_id'], 'is_give' => 0, 'goods_name' => $goodsInfo['goods_name'], 'goods_img' => $goodsInfo['goods_img'], 'goods_num' => $num, 'sale_price' => $goodsInfo['sale_price']];
            if (($num > $goodsInfo['buy_num']) && ($goodsInfo['get_num'] > $goodsInfo['buy_num'])) {
                $newNum = intval($num / $goodsInfo['buy_num']) * $goodsInfo['get_num'];
                $pamas['goods_list'][] = ['goods_id' => $goodsInfo['goods_id'], 'is_give' => 1, 'goods_name' => $goodsInfo['goods_name'], 'goods_img' => $goodsInfo['goods_img'], 'goods_num' => $newNum, 'sale_price' => $goodsInfo['sale_price']];
            }
            $pamas['mini_quantity'] = $goodsInfo['mini_quantity'];
            $pamas['money'] = $goodsInfo['sale_price'] * $num;
            $pamas['sale_price'] = $goodsInfo['sale_price'];
            $pamas['num'] = $newNum + $num;
            return $pamas;
        }
        return false;
    }
}
